/*
 * @ClassName show.dart
 * @Description 封装一些弹窗进行封装
 * @Author SuPeiSen
 * @Date 2020/7/14 17:52
 */

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_su/common/global_context.dart';
import 'package:flutter_su/common/info.dart';
import 'package:flutter_su/config/app_config.dart';
import 'package:flutter_su/enum/language_type.dart';

import 'fun.dart';

class Show {
  // 错误的弹窗
  static void showMsgByError(String msg) {
    showMsg(msg, bgColor: AppSetting.UN_SUCCESS_COLOR);
  }

  // 成功的弹窗
  static void showMsgBySuccess(String msg) {
    showMsg(msg, bgColor: AppSetting.SUCCESS_COLOR);
  }

  // 错误的弹窗
  static void showMsgByProviderError({
    ContentType contentType = ContentType.show,
    String key = "unSuccess",
  }) {
    showMagByProvider(
        bgColor: AppSetting.UN_SUCCESS_COLOR,
        contentType: contentType,
        key: key);
  }

  // 成功的弹窗
  static void showMsgByProviderSuccess({
    ContentType contentType = ContentType.show,
    String key = "success",
  }) {
    showMagByProvider(
        bgColor: AppSetting.SUCCESS_COLOR, key: key, contentType: contentType);
  }

  static void showMagByProvider({
    Color bgColor = Colors.black,
    Color textColor = Colors.white,
    bool userInteractions = true,
    ContentType contentType = ContentType.show,
    @required String key,
  }) {
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = Colors.black // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    var textStyle = TextStyle(
      color: textColor,
      fontSize: SFunCommon.fontSize(30),
      decoration: TextDecoration.none,
    );
    Widget providerText =
        SFunCommon.text(textStyle, contentType: contentType, key: key);

    Widget widget = Container(
      color: Color.fromRGBO(0, 0, 0, 0),
      width: SFunCommon.width(500),
      alignment: Alignment.center,
      child: Container(
        color: bgColor,
        padding: EdgeInsets.only(
          left: SFunCommon.width(20),
          right: SFunCommon.width(20),
          top: SFunCommon.height(10),
          bottom: SFunCommon.height(10),
        ),
        child: providerText,
      ),
    );

    EasyLoading.show(
      status: "",
      indicator: widget,
    );

    Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
  }

  // 文字提示
  static void showMsg(
    String msg, {
    Color bgColor = Colors.black,
    Color textColor = Colors.white,
    bool userInteractions = true,
  }) {
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = Colors.black // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    Widget widget = Container(
      color: Color.fromRGBO(0, 0, 0, 0),
      width: SFunCommon.width(500),
      alignment: Alignment.center,
      child: Container(
        color: bgColor,
        padding: EdgeInsets.only(
          left: SFunCommon.width(20),
          right: SFunCommon.width(20),
          top: SFunCommon.height(10),
          bottom: SFunCommon.height(10),
        ),
        child: Text(
          msg,
          style: TextStyle(
            color: textColor,
            fontSize: SFunCommon.fontSize(30),
            decoration: TextDecoration.none,
          ),
        ),
      ),
    );

    EasyLoading.show(
      status: "",
      indicator: widget,
    );

    Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
  }

  // loading
  static void loading({
    String msg = "loading",
    Color bgColor = Colors.black,
    Color textColor = Colors.white,
    Function iconOnTap,
    bool userInteractions = false,
    Color indicatorColor = Colors.white,
    bool autoDismiss = false,
  }) {
    // 设置弹出框的色调
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = indicatorColor // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    // 指示器
    Widget indicator = Container(
      child: SpinKitFadingCircle(
        color: Colors.black,
        size: SFunCommon.width(90),
      ),
    );

    Widget center = Container(
      width: SFunCommon.width(180),
      height: SFunCommon.width(180),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
              color: Colors.black12,
              offset: Offset(0.0, 15.0), //阴影xy轴偏移量
              blurRadius: 15.0, //阴影模糊程度
              spreadRadius: 1.0 //阴影扩散程度
              )
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          indicator,
          Text(
            msg,
            style: SFunCommon.fontStyle(
              textColor,
              fontSize: SFunCommon.fontSize(40),
            ),
          ),
        ],
      ),
    );

    Widget deleteIcon = GestureDetector(
      onTap: () {
        dismiss();
        if (iconOnTap != null) {
          iconOnTap();
        }
      },
      child: Container(
        color: Color.fromRGBO(0, 0, 0, 0),
        width: SFunCommon.width(50),
        height: SFunCommon.width(50),
        child: Icon(
          Icons.close,
          color: textColor,
          size: SFunCommon.width(50),
        ),
      ),
    );

    Widget widget = Container(
      width: SFunCommon.width(220),
      height: SFunCommon.width(220),
      color: Color.fromRGBO(255, 255, 255, 0),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          center,
          Positioned(
            right: SFunCommon.width(0),
            top: SFunCommon.width(0),
            child: deleteIcon,
          ),
        ],
      ),
    );

    if (autoDismiss) {
      Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
    }

    EasyLoading.show(
      status: "",
      indicator: widget,
    );
  }

  // loading
  static void loadingByProvider({
    Color bgColor = Colors.black,
    Color textColor = Colors.white,
    Function iconOnTap,
    bool userInteractions = false,
    Color indicatorColor = Colors.white,
    bool autoDismiss = false,
    ContentType contentType = ContentType.show,
    @required String key,
  }) {
    // 设置弹出框的色调
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = indicatorColor // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    // 指示器
    Widget indicator = Container(
      child: SpinKitFadingCircle(
        color: Colors.black,
        size: SFunCommon.width(90),
      ),
    );

    var textStyle = TextStyle(
      color: textColor,
      fontSize: SFunCommon.fontSize(30),
      decoration: TextDecoration.none,
    );
    Widget providerText =
        SFunCommon.text(textStyle, contentType: contentType, key: key);

    Widget center = Container(
      width: SFunCommon.width(180),
      height: SFunCommon.width(180),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
              color: Colors.black12,
              offset: Offset(0.0, 15.0), //阴影xy轴偏移量
              blurRadius: 15.0, //阴影模糊程度
              spreadRadius: 1.0 //阴影扩散程度
              )
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          indicator,
          providerText,
        ],
      ),
    );

    Widget deleteIcon = GestureDetector(
      onTap: () {
        dismiss();
        if (iconOnTap != null) {
          iconOnTap();
        }
      },
      child: Container(
        color: Color.fromRGBO(0, 0, 0, 0),
        width: SFunCommon.width(50),
        height: SFunCommon.width(50),
        child: Icon(
          Icons.close,
          color: textColor,
          size: SFunCommon.width(50),
        ),
      ),
    );

    Widget widget = Container(
      width: SFunCommon.width(220),
      height: SFunCommon.width(220),
      color: Color.fromRGBO(255, 255, 255, 0),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          center,
          Positioned(
            right: SFunCommon.width(0),
            top: SFunCommon.width(0),
            child: deleteIcon,
          ),
        ],
      ),
    );

    if (autoDismiss) {
      Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
    }

    EasyLoading.show(
      status: "",
      indicator: widget,
    );
  }

  // 显示错误
  static void error({String msg = ""}) {
    Widget error = Icon(
      Icons.close,
      size: 50,
      color: Colors.white,
    );
    _show(msg, widget: error, bgColor: Colors.redAccent);
  }

  // 显示错误
  static void warn({String msg = ""}) {
    Widget error = Icon(
      Icons.error,
      size: 50,
      color: Colors.white,
    );
    _show(msg, widget: error, bgColor: Colors.amber);
  }

  // 成功显示
  static void success({String msg = ""}) {
    Widget success = Icon(
      Icons.done,
      size: 50,
      color: Colors.white,
    );
    _show(msg, widget: success, bgColor: Colors.green);
  }

  // 显示错误
  static void warnByProvider({ContentType contentType = ContentType.show, String key}) {
    Widget error = Icon(
      Icons.close,
      size: 50,
      color: Colors.white,
    );
    _showByProvider(
        widget: error,
        bgColor: Colors.redAccent,
        contentType: contentType,
        key: key ?? "unSuccess");
  }

  // 成功显示
  static void successByProvider(
      {ContentType contentType = ContentType.show, String key}) {
    Widget error = Icon(
      Icons.close,
      size: 50,
      color: Colors.white,
    );
    _showByProvider(
        widget: error,
        bgColor: Colors.redAccent,
        contentType: contentType,
        key: key ?? "success");
  }

  // 显示错误
  static void errorByProvider({ContentType contentType = ContentType.show, String key}) {
    Widget error = Icon(
      Icons.close,
      size: 50,
      color: Colors.white,
    );
    _showByProvider(
        widget: error,
        bgColor: Colors.redAccent,
        contentType: contentType,
        key: key ?? "unSuccess");
  }

  static void _show(
    String msg, {
    @required Widget widget,
    Color bgColor = Colors.green,
    Color textColor = Colors.white,
    Function iconOnTap,
    bool userInteractions = true,
    Color indicatorColor = Colors.white,
    bool autoDismiss = true,
  }) {
    // 设置弹出框的色调
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = indicatorColor // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    Widget text = Container(
      padding: EdgeInsets.only(
        left: SFunCommon.width(10),
        right: SFunCommon.width(10),
        bottom: SFunCommon.height(20),
        top: SFunCommon.height(20),
      ),
      child: Text(
        msg,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: textColor,
          fontSize: SFunCommon.fontSize(30),
          decoration: TextDecoration.none,
        ),
      ),
    );

    Widget box = Container(
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
              color: Colors.black12,
              offset: Offset(0.0, 15.0), //阴影xy轴偏移量
              blurRadius: 15.0, //阴影模糊程度
              spreadRadius: 1.0 //阴影扩散程度
              )
        ],
      ),
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: SFunCommon.width(200),
          maxWidth: SFunCommon.width(500),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                top: SFunCommon.height(15),
              ),
              child: widget,
            ),
            text,
          ],
        ),
      ),
    );

    if (autoDismiss) {
      Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
    }

    EasyLoading.show(
      status: "",
      indicator: box,
    );
  }

  static void _showByProvider({
    @required Widget widget,
    Color bgColor = Colors.green,
    Color textColor = Colors.white,
    Function iconOnTap,
    bool userInteractions = true,
    Color indicatorColor = Colors.white,
    bool autoDismiss = true,
    ContentType contentType = ContentType.show,
    @required String key,
  }) {
    // 设置弹出框的色调
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 自定义显示弹窗内容
      ..textColor = Colors.white // 文字颜色
      ..fontSize = SFunCommon.fontSize(20) // 文字大小
      ..indicatorColor = indicatorColor // 指示器的颜色
      ..progressColor = Colors.white // 进度条指示器的颜色
      ..userInteractions = userInteractions // 当loading展示的时候，是否允许用户操作
      ..maskColor = Color.fromRGBO(255, 255, 255, 0) // 遮罩的背景色
      ..backgroundColor = Color.fromRGBO(0, 0, 0, 0); // 背景颜色

    var textStyle = TextStyle(
      color: textColor,
      fontSize: SFunCommon.fontSize(30),
      decoration: TextDecoration.none,
    );
    Widget providerText =
        SFunCommon.text(textStyle, contentType: contentType, key: key);

    Widget text = Container(
      padding: EdgeInsets.only(
        left: SFunCommon.width(10),
        right: SFunCommon.width(10),
        bottom: SFunCommon.height(20),
        top: SFunCommon.height(20),
      ),
      child: providerText,
    );

    Widget box = Container(
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
              color: Colors.black12,
              offset: Offset(0.0, 15.0), //阴影xy轴偏移量
              blurRadius: 15.0, //阴影模糊程度
              spreadRadius: 1.0 //阴影扩散程度
              )
        ],
      ),
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: SFunCommon.width(200),
          maxWidth: SFunCommon.width(500),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                top: SFunCommon.height(15),
              ),
              child: widget,
            ),
            text,
          ],
        ),
      ),
    );

    if (autoDismiss) {
      Future.delayed(Duration(seconds: 2)).then((_) => dismiss());
    }

    EasyLoading.show(
      status: "",
      indicator: box,
    );
  }

  // 取消弹窗
  static void dismiss() => EasyLoading.dismiss();

  /// 底部弹窗
  static void showBottomSheet({String msg}) {
    BuildContext context = GlobalContext.appContext;

    Widget buttons = Container(
      child: ButtonBar(
        alignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
            child: Text("复制"),
            onPressed: () {
              Clipboard.setData(ClipboardData(text: msg));
//              toast(msg: "成功");
            },
          ),
          RaisedButton(
            child: Text("返回"),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );

    Widget content = Container(
      margin: EdgeInsets.only(
        left: 10,
        right: 10,
      ),
      child: SingleChildScrollView(
        child: Text(
          msg,
          softWrap: true,
        ),
      ),
    );

    Widget box = Column(
      children: <Widget>[
        buttons,
        content,
      ],
    );

    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            alignment: Alignment.center,
            height: double.infinity,
            child: box,
          );
        });
  }
}

// 自定义全局弹窗
class Toast {
  // 是否正在显示
  static bool _isLoading = false;

  // 保存OverlayEntry对象
  static OverlayEntry _overlayEntry;

  // 居中文字显示
  static void showText({
    @required String msg,
    Color bgColor,
    Color textColor,
    double top,
    double textSize,
    Duration duration,
  }) {
    if (_overlayEntry != null) {
      _overlayEntry.remove();
      _isLoading = false;
    }
    Widget text = IntrinsicWidth(
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: bgColor ?? Colors.black,
          borderRadius: BorderRadius.circular(5),
        ),
        padding: EdgeInsets.only(
          left: 20,
          right: 20,
          top: 10,
          bottom: 10,
        ),
        child: Text(
          msg,
          style: SFunCommon.fontStyle(
            textColor ?? Colors.white,
            fontSize: textColor ?? 20,
          ),
        ),
      ),
    );

    _showWidget(widget: text, duration: duration, top: top);
  }

  static void _showWidget(
      {Widget widget, Duration duration, double top}) async {
    bool _show = true;
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return Positioned(
          top: top ?? Info.height / 2,
          child: Container(
            alignment: Alignment.center,
            width: Info.width,
            color: Color.fromRGBO(255, 255, 255, 0), // 背景颜色透明
            child: AnimatedOpacity(
              opacity: _show ? 1.0 : 0.0,
              duration: duration ?? Duration(seconds: 2),
              child: widget,
            ),
          ),
        );
      },
    );

    //显示到屏幕上
    Overlay.of(GlobalContext.appContext).insert(_overlayEntry);

    // 两秒后移除
    Future.delayed(Duration(seconds: 1)).then((_) {
      _show = false;
      _overlayEntry.markNeedsBuild();
//      _overlayEntry = null;
      _isLoading = false;
    });
  }

  // 提供remove方法
  static void removeOverlay() {
    if (_overlayEntry != null) {
      _overlayEntry.remove();
      _overlayEntry = null;
      _isLoading = false;
    }
  }

  // 获取是否正处于loading
  static bool get overlayStatus => _isLoading;
}
